package org.qqcui.designpattern.patterns.mediator;

import java.util.HashMap;
import java.util.Map;

/**
 * 中介者实现类
 *
 * @author cuiqingqiang
 * @version 1.0
 * @date 2022/5/6 17:13
 **/
public class ConcreteMediator extends Mediator {

    private Map<String, Colleague> colleagueMap;

    private Map<String, String> interMap;

    public ConcreteMediator() {
        colleagueMap = new HashMap<>();
        interMap = new HashMap<>();
    }

    @Override
    public void registerColleague(String colleagueName, Colleague colleague) {
        colleagueMap.put(colleagueName, colleague);
        if (colleague instanceof Alarm) {
            interMap.put("Alarm", colleagueName);
        } else if (colleague instanceof CoffeeMachine) {
            interMap.put("CoffeeMachine", colleagueName);
        } else if (colleague instanceof Curtain) {
            interMap.put("Curtain", colleagueName);
        } else if (colleague instanceof TV) {
            interMap.put("TV", colleagueName);
        }
    }

    @Override
    public void getMessage(int stateChange, String colleagueName) {
        if (colleagueMap.get(colleagueName) instanceof Alarm) {
            if (0 == stateChange) {
                ((CoffeeMachine) colleagueMap.get(interMap.get("CoffeeMachine"))).startCoffee();
                ((TV) colleagueMap.get(interMap.get("TV"))).startTV();
            } else if (1 == stateChange) {
                ((TV) colleagueMap.get(interMap.get("TV"))).stopTV();
            }
        } else if (colleagueMap.get(colleagueName) instanceof CoffeeMachine) {
            ((Curtain) colleagueMap.get(interMap.get("Curtain"))).upCurtain();
        } else if (colleagueMap.get(colleagueName) instanceof Curtain) {
            // 省略
        } else if (colleagueMap.get(colleagueName) instanceof TV) {
            // 省略
        }
    }

    @Override
    public void sendMessage() {

    }
}
